/*
 * FeedDateComparator.java
 * 
 * Copyright (C) 2010 no.good.at.coding
 * 
 * This file is part of the NoGoodCommons Library.
 *
 * The NoGoodCommons Library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.nogoodatcoding.commons;

import java.util.Comparator;
import java.util.ResourceBundle;

import com.sun.syndication.feed.synd.SyndEntryImpl;

import org.apache.log4j.*;

/**
 * 
 * A comparator for {@link com.sun.syndication.feed.synd.SyndEntry} objects that compares them on basis of {@code publishedDate}.
 * 
 * @author no.good.at.coding
 * 
 */

public class FeedDateComparator implements Comparator<SyndEntryImpl>
{
    private static Logger log = Logger.getLogger(FeedDateComparator.class);
    private static ResourceBundle bundle = ResourceBundle
            .getBundle("com.nogoodatcoding.commons.messages.Messages_FeedDateComparator");

    private boolean isDescending = true;

    /**
     * 
     * Default constructor for convenience; {@code sortDescending} is assumed to be {@code true}. Will return a comparator that will sort by
     * descending order of timestamps i.e. newest entries first
     * 
     * @see FeedDateComparator#FeedDateComparator(boolean)
     * 
     */
    public FeedDateComparator()
    {
        this.isDescending = true;
    }

    /**
     * 
     * Constructor for {@code FeedDateComparator}.
     * 
     * @param sortDescending
     *            Indicates if the sorting is to be in descending order (newest first) or not (i.e. in descending order (oldest first)).
     * 
     * @see com.nogoodatcoding.commons.FeedDateComparator#FeedDateComparator()
     * 
     */
    public FeedDateComparator(boolean sortDescending)
    {
        this.isDescending = sortDescending;
    }

    public int compare(SyndEntryImpl entry1, SyndEntryImpl entry2)
    {

        if (entry1 == null | entry2 == null)
        {
            log.error(String.format(bundle.getString("log.error.entry.null"), entry1, entry2));
            throw new NullPointerException();
        }

        log.trace(String.format(bundle.getString("log.trace.sort.order"), isDescending));

        String item1 = entry1.getTitle();
        String item2 = entry2.getTitle();

        long timestamp1 = entry1.getPublishedDate().getTime();
        long timestamp2 = entry2.getPublishedDate().getTime();
        log.trace(String.format(bundle.getString("log.trace.times"), timestamp1, timestamp2));

        if (timestamp1 < timestamp2)
        {
            if (isDescending)
            {
                log.trace(String.format("log.trace.return", 1, item2, item1));
                return 1;
            }
            else
            {
                log.trace(String.format("log.trace.return", -1, item1, item2));
                return -1;
            }
        }
        else if (timestamp1 > timestamp2)
        {
            if (isDescending)
            {
                log.trace(String.format("log.trace.return", -1, item1, item2));
                return -1;
            }
            else
            {
                log.trace(String.format("log.trace.return", 1, item2, item1));
                return 1;
            }
        }
        else
        {
            log.trace(String.format("log.trace.return.equals", 0));
            return 0;
        }
    }
}